package com.company;

import java.sql.*;

public class DAL
{
    private static final String JDBC_Driver="com.mysql.jdbc.Driver";

    ///数据库信息
    private static final String DB_URL = "jdbc:mysql://localhost:3306/TestDB";
    private static final String USER = "root";
    private static final String PASS = "sa123";

    public String getDbUrl() {
        return DB_URL;
    }
    public String getUSER() {
        return USER;
    }
    public String getPASS() {
        return PASS;
    }

    public  void ExecuteQuery() throws SQLException,ClassNotFoundException
    {
        Connection connection = null;
        Statement command = null;
        ResultSet rs = null;
        try
        {
            ///注册 JDBC驱动
           Class.forName(JDBC_Driver);

           connection=DriverManager.getConnection(getDbUrl(),getUSER(),getPASS());
            command=connection.createStatement();
            String sql="SELECT FID,FName FROM TUser WHERE FID=36052";
            rs = command.executeQuery(sql);

            //展开结果集
            while (rs.next())
            {
                int id = rs.getInt("FID");
                String name = rs.getString("FName");

                System.out.println(id+ name);
            }

            ///完成后,关闭
            //rs.close();
            //command.close();
            //connection.close();
        }
        finally {
            if( rs != null) { rs.close(); }
            if(command != null) { command.close(); }
            if(connection != null){ connection.close(); }
        }
    }
}

